﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace NEMproject
{

   
    public partial class WebBrowserForm : Form
    {

        string URL = "http://re.jrc.ec.europa.eu/pvgis/apps4/pvest.php";
       
        public delegate void ChangeControlValuesHandler(string ControlName, dynamic value);
        private ChangeControlValuesHandler changeControlHandler;
        
        private bool documentLoaded;


        public WebBrowserForm()
        {
            InitializeComponent();

            documentLoaded = false;
            
        }


        #region FormFunctions

        public void setDelegate(ChangeControlValuesHandler handler)
        {
            changeControlHandler = handler;        
        }


        private void setDomObjects()
        {

            HtmlElementCollection inputColl = webBrowser.Document.GetElementsByTagName("input");
            HtmlElementCollection formColl = webBrowser.Document.GetElementsByTagName("form");

            inputColl[0].SetAttribute("value", "Nicosia"); //TODO: get location from fromInterface

            //object[] args = { "showAddress(this.address.value);" };
            //string retu = webBrowser.Document.InvokeScript("eval", args).ToString();

            if (inputColl[1].GetAttribute("value").Equals("Search"))
            {
               // inputColl[1].Focus();
               // inputColl[1].RaiseEvent("onsubmit");

            }

            //select tab manthly radiation
            HtmlElement monthlyRadiation = webBrowser.Document.GetElementById("tab2");
            monthlyRadiation.InvokeMember("click");

            //check proper checkboxes in the tab
            HtmlElement horirrad = webBrowser.Document.GetElementById("horirrad");            
            if (horirrad.GetAttribute("checked").Equals("True")) {
                horirrad.InvokeMember("click");
            }
            HtmlElement optrad = webBrowser.Document.GetElementById("optrad");//this we want it ckecked
            if (optrad.GetAttribute("checked").Equals("False"))
            {
                optrad.InvokeMember("click");
            }
            HtmlElement mr_dni = webBrowser.Document.GetElementById("mr_dni");
            if (mr_dni.GetAttribute("checked").Equals("True"))
            {
                mr_dni.InvokeMember("click");
            }
            HtmlElement selectrad = webBrowser.Document.GetElementById("selectrad");
            if (selectrad.GetAttribute("checked").Equals("True"))
            {
                selectrad.InvokeMember("click");
            }
            HtmlElement linketurb = webBrowser.Document.GetElementById("linketurb");
            if (linketurb.GetAttribute("checked").Equals("True"))
            {
                linketurb.InvokeMember("click");
            }
            HtmlElement d2g = webBrowser.Document.GetElementById("d2g");
            if (d2g.GetAttribute("checked").Equals("True"))
            {
                d2g.InvokeMember("click");
            }
            HtmlElement optincl = webBrowser.Document.GetElementById("optincl");
            if (optincl.GetAttribute("checked").Equals("True"))
            {
                optincl.InvokeMember("click");
            }
            HtmlElement avdaytemp = webBrowser.Document.GetElementById("avdaytemp");
            if (avdaytemp.GetAttribute("checked").Equals("True"))
            {
                avdaytemp.InvokeMember("click");
            }
            HtmlElement avtemp = webBrowser.Document.GetElementById("avtemp");
            if (avtemp.GetAttribute("checked").Equals("True"))
            {
                avtemp.InvokeMember("click");
            }
            HtmlElement degreedays = webBrowser.Document.GetElementById("degreedays");
            if (degreedays.GetAttribute("checked").Equals("True"))
            {
                degreedays.InvokeMember("click");
            }
            HtmlElement plotdatagraph_MR = webBrowser.Document.GetElementById("plotdatagraph_MR");
            if (plotdatagraph_MR.GetAttribute("checked").Equals("True"))
            {
                plotdatagraph_MR.InvokeMember("click");
            }
            HtmlElement showhorizon_MR = webBrowser.Document.GetElementById("showhorizon_MR");
            if (showhorizon_MR.GetAttribute("checked").Equals("True"))
            {
                showhorizon_MR.InvokeMember("click");
            }

            //check output method
            foreach (HtmlElement elem in inputColl)
            {
                if (elem.GetAttribute("name").Equals("outputchoicebuttons") && elem.GetAttribute("value").Equals("window"))
                {
                    elem.InvokeMember("click");
                }
            }
            
            
            //formColl[0].InvokeMember("submit");

            //webBrowser.Navigate("javascript:document.forms[0].submit()");



        }



        #endregion



        private void webBrowserForm_Load(object sender, EventArgs e)
        {
            webBrowser.Navigate(URL);
        }

        private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            this.Text = "Loading...";
        }
        
        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.Text = e.Url.ToString();//set title to url

            if (!documentLoaded){
                
                setDomObjects();
                documentLoaded = true;

            }


        }
    }
}
